<?php

class JobscenterController extends Controller
{
	  public $layout = '//layouts/sp';
    public function actionIndex(){
         if(Yii::app()->request->isAjaxRequest)
        echo    $this->renderPartial('/jobs/_center_ajax');
            else
        $this->render('/jobs/center');
    }
    public function actionJobReport(){
        $this->layout='//layout/space';
        $data=$this->getListAllData(-1);
        $this->render('jobreport',array(
            'data'=>$data
        ));
        
    }
   public function getListAllData($page=NULL, $status=null,$bstatus=null, $sort=NULL, $query=NULL)
    {

        $user = Users::model()->findByPk(Yii::app()->user->getId());
        $empno = $user->employeeid;     
     
        $order = CJSON::decode($sort);
        $crit = new CDbCriteria();
        if ($sort != NULL)
        {
            $crit->order = $order[0]['property'] . ' ' . $order[0]['direction'];
        }
        else
        {            
            $crit->order = 'jobstatus ASC,CAST( SUBSTR( jobid, 1,LOCATE(  "-", jobid )-1  ) AS SIGNED) ,CAST( SUBSTR( jobid, LOCATE(  "-", jobid ) +1 ) AS SIGNED )';
        }
        $crit->alias = 'j';
        if ($query != NULL)
        {

            $crit->join = 'left join tbl_locations l on l.locno = j.locno and l.custno = j.custno
                        left join tbl_jobs_status s on s.id = j.jobstatus
                        left join tbl_jobs_billing_status b on b.id = j.billingstatus
                ';
            $crit->condition = '
                j.jobid = :query or 
                j.description like :like or 
                s.description like :like or 
                b.description like :like or 
                l.add1 like :like'
                ;
            $crit->params = array(
                'query' => $query,
                'like' => "%$query%",
            );
        }
        if ($status != NULL)
        {
            $crit->addCondition('j.jobstatus=:status');
            $crit->params['status'] = $status;
        }
        if ($bstatus)
        {
            $crit->addCondition('j.billingstatus=:bstatus');
            $crit->params['bstatus'] = $bstatus;
        }

        $crit->addCondition('j.jobsalesperson=:empno');
        $crit->params['empno'] = $empno;
        
        $pagination = new CPagination();
        if ($page > 0)
            $page--;
        if ($page >= 0)
        {

            $pagination->setPageSize(20);

            $pagination->setCurrentPage($page);
        }
        else
        {
            $pagination->setPageSize(999999999);

            $pagination->setCurrentPage($page);
        }

        $data = new CActiveDataProvider('Jobs', array(
                    'criteria' => $crit,
                    'pagination' => $pagination
                ));
        $rs = array();
        if ($data != NULL && count($data->getData()) > 0)
        {

            $rs['totalCount'] = $data->getTotalItemCount();
            foreach ($data->getData() as $i => $item)
            {

                $rs['data'][$i] = $item->getAttributes();
                $location = $item->location;
                if ($location != NULL)
                {
                    $rs['data'][$i]['locno'] = $location->locno;
                    $rs['data'][$i]['add1'] = $location->add1;
                    $rs['data'][$i]['csz'] = $location->city . ' ' . $location->state . ' ' . $location->zip;
                    $rs['data'][$i]['locname'] = $location->locname;
                }
                else
                {
                    $rs['data'][$i]['locname'] = NULL;
                }

                $rs['data'][$i]['billingstatustext'] = JobsBillingStatus::getText($item->billingstatus);
                $rs['data'][$i]['jobstatustext'] = JobsStatus::getText($item->jobstatus);
            }
        }
        return $rs;
    }

    public function actionListAll($page=0, $status=null,$bstatus=null, $sort=NULL, $query=NULL)
    {
        echo CJSON::encode($this->getListAllData($page, $status,$bstatus, $sort, $query));
    }

}

?>
